﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PGI.EN.Inventario;
using PGI.LN.Inventario;

namespace PGI.WF
{
    public partial class FrmInventarioProductoDetalle : Form
    {
        public int ProductoId = 0;
        public InventarioProductoDetalleEn _invVentasDetEn = null;


        public FrmInventarioProductoDetalle()
        {
            InitializeComponent();
        }

        private void FrmInventarioVentasDetalle_Load(object sender, EventArgs e)
        {
            dgwInsVentaDetalle.DataSource = InventarioProductoDetalleLn.SelInventarioDetalleProducto(ProductoId);
            dgwInsVentaDetalle.ClearSelection();
            
            dgwInsVentaDetalle.Columns[0].Visible = false;
            dgwInsVentaDetalle.Columns[1].Visible = false;
            dgwInsVentaDetalle.Columns[3].Visible = false;
            dgwInsVentaDetalle.Columns[5].Visible = false;
            dgwInsVentaDetalle.Columns[7].Visible = false;
        }
        
        private void FrmInventarioVentasDetalle_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                btnCerrar_Click(sender, e);

            if (e.KeyCode == Keys.F1)
                btnInsertar_Click(sender, e);
        }

        private void btnInsertar_Click(object sender, EventArgs e)
        {
            if (dgwInsVentaDetalle.Rows.Count > 0)
            {
                if (_invVentasDetEn != null)
                {
                    if (FrmInventarioVentas.Instance() != null)
                        FrmInventarioVentas.Instance().InvVentasDetalle = _invVentasDetEn;

                    if (FrmInventarioBajas.Instance() != null)
                        FrmInventarioBajas.Instance().InvVentasDetalle = _invVentasDetEn;
                }

                //_invVentasDetEn = null;

                this.Close();
            }
            else
                MessageBox.Show(this, "No se han configurado detalles de producto");
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show(this, "¿Desea salir de la ventana sin guardar cambios?", "Pregunta", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
                this.Dispose();
        }

      

        private void dgwInsVentaDetalle_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dgwInsVentaDetalle.ClearSelection();
            _invVentasDetEn = null;
        }

        private void dgwInsVentaDetalle_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex != -1)
                {
                    _invVentasDetEn = new InventarioProductoDetalleEn();

                    _invVentasDetEn.ProductoID = Convert.ToInt32(dgwInsVentaDetalle.Rows[e.RowIndex].Cells[0].Value);
                    _invVentasDetEn.TallaID = Convert.ToInt32(dgwInsVentaDetalle.Rows[e.RowIndex].Cells[1].Value);
                    _invVentasDetEn.TallaDes = dgwInsVentaDetalle.Rows[e.RowIndex].Cells[2].Value.ToString();
                    _invVentasDetEn.ColorID = Convert.ToInt32(dgwInsVentaDetalle.Rows[e.RowIndex].Cells[3].Value);
                    _invVentasDetEn.ColorDes = dgwInsVentaDetalle.Rows[e.RowIndex].Cells[4].Value.ToString();
                    _invVentasDetEn.MaterialID = Convert.ToInt32(dgwInsVentaDetalle.Rows[e.RowIndex].Cells[5].Value);
                    _invVentasDetEn.MaterialDes = dgwInsVentaDetalle.Rows[e.RowIndex].Cells[6].Value.ToString();
                    _invVentasDetEn.TemporadaID = Convert.ToInt32(dgwInsVentaDetalle.Rows[e.RowIndex].Cells[7].Value);
                    _invVentasDetEn.TemporadaDes = dgwInsVentaDetalle.Rows[e.RowIndex].Cells[8].Value.ToString();
                    _invVentasDetEn.Stock = Convert.ToInt32(dgwInsVentaDetalle.Rows[e.RowIndex].Cells[9].Value);
                    _invVentasDetEn.Costo_Unitario = Convert.ToDouble(dgwInsVentaDetalle.Rows[e.RowIndex].Cells[11].Value);
                    _invVentasDetEn.Precio_Venta = Convert.ToDouble(dgwInsVentaDetalle.Rows[e.RowIndex].Cells[12].Value);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        

        

       

        
    }
}
